import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#标准hough变换 HoughLines
img = cv.imread('./qipan.jpg') #读图
gray = cv.cvtColor(img,cv.COLOR_BGR2GRAY)#灰度图转换
edges = cv.Canny(gray, 50, 150, apertureSize=3)#canny边缘化
lines = cv.HoughLines(edges, 1, np.pi/180, 200)#直线检测
#lines = cv.HoughLinesP(edges, 1, np.pi/180, 60, minLineLength=60, maxLineGap=10)

lines1 = lines[:, 0, :] #提取为为二维
for rho, theta in lines1[:]:
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a*rho
    y0 = b*rho
    x1 = int(x0 + 1000*(-b))
    y1 = int(y0 + 1000*(a))
    x2 = int(x0 - 1000*(-b))
    y2 = int(y0 - 1000*(a))
    cv.line(edges, (x1, y1), (x2, y2), (255, 0, 0), 1)

cv.imshow("HoughLines", edges)


'''
plt.subplot(122), plt.imshow(img,)
plt.xticks([]), plt.yticks([])
'''
minLineLength = 60
maxLineGap = 10
lines = cv.HoughLinesP(edges, 1, np.pi/180, 18, minLineLength, maxLineGap)
for x in range(0, len(lines)):
    for x1, y1, x2, y2 in lines[x]:
        cv.line(edges, (x1, y1), (x2, y2), (0, 255, 0), 2)

cv.imshow('houghP', edges)
cv.waitKey(0)
cv.destroyAllWindows()
#累计huogh变换 HoughLinesP
